from django import forms
from .models import Course, Homework, Handin, Comment
from django.forms import ModelForm
from ckeditor_uploader.widgets import CKEditorUploadingWidget


class RegistrationForm(forms.Form):
    role = forms.ChoiceField(label='角色', choices=((0, '学生'), (1, '教师')),
                             widget=forms.widgets.Select(attrs={'class': 'form-control'}))


class ProfileForm(forms.Form):
    name = forms.CharField(label='姓名', max_length=50, required=False)
    gender = forms.ChoiceField(label='性别', widget=forms.Select(), choices=([('0', '男'), ('1', '女'), ]), required=False)
    description = forms.CharField(label='个人描述', max_length=50, required=False)


class CourseForm(forms.ModelForm):
    class Meta:
        model = Course
        # 选择指定字段的所有数据 field
        fields = ['cname', 'classes', 'description']
        # boostrap表单需要 form-control 这个样式
        widgets = {
            'cname': forms.TextInput(attrs={'class': 'form-control'}),
            'classes': forms.TextInput(attrs={'class': 'form-control'}),
            'description': forms.Textarea(attrs={'class': 'form-control', 'rows': '3'})
        }


class HomeworkForm(ModelForm):
    class Meta:
        model = Homework
        # 剔除指定字段的所有数据 exclude
        exclude = ['author', 'views', 'slug', 'published', 'course']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            # 'body': forms.Textarea(attrs={'class': 'form-control'}),
            'body': CKEditorUploadingWidget(attrs={'class': 'form-control'}),
            'status': forms.Select(attrs={'class': 'form-control'}),
            'file': forms.ClearableFileInput(attrs={'class': 'form-control'}),
        }
        labels = {
            'title': '作业标题',
            'body': '作业内容',
            'status': '作业状态',
            'group': '组队状态',
            'file': '上传文件',
        }

    def clean_file(self):
        file = self.cleaned_data['file']
        if file:
            ext = file.name.split('.')[-1].lower()
            if ext not in ["", "jpg", "png", "pdf", "xlsx", "docx", "zip", "doc"]:
                raise forms.ValidationError("Only zip jpg, png, pdf, doc, docx, and xlsx files are allowed.")
            return file


class HandinForm(ModelForm):
    class Meta:
        model = Handin
        exclude = ['author', 'homework', 'course', 'score']
        widgets = {
            'body': CKEditorUploadingWidget(attrs={'class': 'form-control'}),
            # 'file': forms.FileInput(attrs={'class': 'form-control'}),
            'file': forms.ClearableFileInput(attrs={'class': 'form-control'}),
        }

    def clean_file(self):
        file = self.cleaned_data['file']
        if file:
            ext = file.name.split('.')[-1].lower()
            if ext not in ["", "jpg", "png", "pdf", "xlsx", "docx", "zip", "doc"]:
                raise forms.ValidationError("Only zip jpg, png, pdf, doc, docx, and xlsx files are allowed")
            return file


class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['text']
        widgets = {
            'text': forms.Textarea(attrs={'class': 'form-control', 'rows': '3'}),
        }
        labels = {
            'text': '评论内容',
        }
